package com.clz.latte.ec.main.Index.adapter;

import android.support.v7.widget.GridLayoutManager;
import android.widget.ImageView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.clz.latte.ec.R;
import com.clz.latte.ec.entity.IndexBean;
import com.clz.latte.ec.entity.MulutipleType;
import com.clz.latte.ui.banner.BannerCreator;

import java.util.List;

/**
 * Created by Administrator on 2018/3/27.
 */

public class IndexAdapter extends BaseMultiItemQuickAdapter<IndexBean, BaseViewHolder> implements BaseQuickAdapter.SpanSizeLookup, OnItemClickListener {
    private static final RequestOptions REQUEST_OPTIONS
            = new RequestOptions()
            .centerCrop()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .dontAnimate();
    //确保初始化一次Banner，防止重复Item加载
    private boolean mIsInitBanner = false;

    public IndexAdapter(List<IndexBean> data) {
        super(data);
        init();
    }

    private void init() {
        addItemType(MulutipleType.TEXT, R.layout.item_multiple_text);
        addItemType(MulutipleType.IMAGE, R.layout.item_multiple_image);
        addItemType(MulutipleType.TEXT_IMAGE, R.layout.item_multiple_image_text);
        addItemType(MulutipleType.BANNER, R.layout.item_multiple_banner);
        //设置宽度监听
        setSpanSizeLookup(this);
        openLoadAnimation();
        isFirstOnly(false);
    }

    @Override
    protected void convert(BaseViewHolder helper, IndexBean item) {
        final int itemViewType = helper.getItemViewType();
        final List<String> bannerImages;
        final String imageUrl = item.getImageUrl();
        switch (itemViewType) {
            case MulutipleType.TEXT:
                helper.setText(R.id.text_single, item.getText());
                break;
            case MulutipleType.TEXT_IMAGE:
                helper.setText(R.id.tv_multiple, item.getText());
                Glide.with(mContext)
                        .load(item.getImageUrl())
                        .apply(REQUEST_OPTIONS)
                        .into((ImageView) helper.getView(R.id.img_multiple));
                break;
            case MulutipleType.IMAGE:
                Glide.with(mContext)
                        .load(imageUrl)
                        .apply(REQUEST_OPTIONS)
                        .into((ImageView) helper.getView(R.id.img_single));
                break;
            case MulutipleType.BANNER:
                if (!mIsInitBanner) {
                    bannerImages = item.getBanners();
                    final ConvenientBanner<String> convenientBanner = helper.getView(R.id.banner_recycler_item);
                    BannerCreator.setDefault(convenientBanner, bannerImages, this);
                    mIsInitBanner = true;
                }
                break;
            default:
                break;
        }
    }

    @Override
    public int getSpanSize(GridLayoutManager gridLayoutManager, int position) {
        return getData().get(position).getSpanSize();
    }

    @Override
    public void onItemClick(int position) {

    }
}
